#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
# Date: 2018/10/29

import pandas as pd


############出行温馨提示基本参数###############
class Rule(object):
    def __init__(self,set_weater,air_now,air_flag,data_now):
        self.set_weater = set_weater

    def get_code(self):
        #获取天气对应表
        code_data = pd.read_excel('data/weater.xlsx')
        return code_data

    def do_change(self,a,b):
        ##白天早上天气想同为0，不相同为1
        if int(a)== int(b):
            return 0
        else:
            return 1

    def data_change(self):
        #将天气特征转化为中文格式
        code_data = self.get_code()
        cond =  list(map(lambda x,y:self.do_change(x,y),self.set_weater.cond_code_n, self.set_weater.cond_code_d))
        weater = []
        for index in self.set_weater.index:
            data =  self.set_weater.iloc[index,:]
            cond_txt_d = code_data[code_data['Code'] == int(data['cond_code_d'])]['China'].tolist()[0]
            if cond[index] == 1:
                cond_txt_n = code_data[code_data['Code'] == int(data['cond_code_n'])]['China'].tolist()[0]

                weater.append(cond_txt_d+'转'+cond_txt_n)
            else:
                weater.append(cond_txt_d)
        self.set_weater['weater'] = weater



    def rain(self):

        ##白天
        self.data_change()

        ##判断是否有雨，4天数据

        rain_pd = pd.DataFrame(columns=self.set_weater.columns)
        for index in range(4):
            data = self.set_weater.iloc[index,:]
            if '雨' in data['weater']:
                df_i = pd.DataFrame([data], columns=self.set_weater.columns)
                rain_pd = rain_pd.append(df_i)

        return rain_pd


    def specical_weater(self):
        ###特殊天气
        ts_weater = ['极端降雨','暴雨','冰雹','冻雨','暴雪','霾','沙尘暴','雾']
        special_pd = pd.DataFrame(columns=self.set_weater.columns)
        for indexs in self.set_weater.index:
            weater =  self.set_weater.ix[indexs]
            for ts in ts_weater:

                if ts in weater['weater']:
                    weater_d = weater['weater'].split('转')
                    if len(weater_d)>1:
                        weater_n = weater['weater'].split('转')[1]
                        if  ts in  weater_n:
                            weater['weater'] = weater_n
                        else:
                            weater['weater'] = weater_d[0]
                    df_i = pd.DataFrame([weater], columns=self.set_weater.columns)
                    special_pd = special_pd.append(df_i)

        ##日期转变
        if len(ts_weater):
            special_pd['time'] = [x.split('-')[1]+'月'+x.split('-')[2]+'日' for x in special_pd['date'].tolist()]

        return special_pd

    def weater_hight(self):
        weater  = self.set_weater.iloc[:4,:]

        date = weater['tmp_max'].apply(lambda x:1 if int(x) >= 33 else 0)
        weater.insert(0, 'high_wd', date)
        return weater


    def weater_comparison(self):
        ##跟前一天的温度对比
        df2 = self.set_weater.iloc[:-1, :].reset_index().iloc[:, 1:]#昨天的数据
        df1 = self.set_weater.iloc[1:, :].reset_index().iloc[:, 1:]#

        df2.columns = df2.columns.map(lambda x: x + '_1')
        set_weater_wt = pd.concat([df1, df2], axis=1)


        set_weater_wt['flu_max'] = set_weater_wt['tmp_max'].astype(int) - set_weater_wt['tmp_max_1'].astype(int)
        set_weater_wt['flu_min'] = set_weater_wt['tmp_min'].astype(int)  -set_weater_wt['tmp_min_1'].astype(int)


        return set_weater_wt.loc[:3]


